package Dao;

import entity.Game;

import java.sql.*;
import java.util.ArrayList;

public class DbDao extends Game {

    private   String driver = "com.mysql.cj.jdbc.Driver";//定义驱动
    private   String url = "jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=true";//定义URL
    private   String databaseUser = "root";//定义用户名
    private   String password = "123456wfz";//定义密码

    DbDao DbDao() {
        return this;
    }

    public ArrayList getGames() {
        ArrayList arrayList = new ArrayList();
        try {
            Class.forName(driver);
            Connection con = DriverManager.getConnection(url, databaseUser, password);

            Statement statement = con.createStatement();
            String sql = "SELECT * FROM game";//定义一个查询语句
            ResultSet rs = statement.executeQuery(sql);//执行查询语句
            while (rs.next()) {
                Game game = new Game();
                game.setId(rs.getInt("id"));
                game.setCoverurl(rs.getString("coverurl"));
                game.setName(rs.getString("name"));
                game.setIntroduction(rs.getString("introduction"));
                game.setLabel(rs.getString("label"));
                game.setDate(rs.getDate("date"));
                arrayList.add(game);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return arrayList;

        }
    }
}
